#include"threadPool.h"
void unlock(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    pthread_mutex_unlock(&pthreadPool->mutex);
}
void *threadFunc(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    while(1){
        int netfd;
        pthread_mutex_lock(&pthreadPool->mutex);
        pthread_cleanup_push(unlock,pthreadPool);
        while(pthreadPool->taskQueue.queueSize == 0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        netfd = pthreadPool->taskQueue.pFront->netfd;
        printf("I am worker, I got netfd = %d\n",netfd);
        deQueue(&pthreadPool->taskQueue);
        pthread_cleanup_pop(1);

        printf("I am worker doing something\n");

        //文件传输函数
        //transfile(netfd);
        //handle_command(netfd,"500M_test",2);
        clientHandler(netfd);
        
    }
}

int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->tidArr.workerNum;++i){
        pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,pthreadPool);
    }
    return 0;
}

